<?php

namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;
use PHPExcel;
use PHPExcel_Writer_Excel5;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Fill;
use PHPExcel_Style_Border;
use PHPExcel_Cell_DataType;

class Wanyongapply extends Commonwork
{
	public function daochufull($aid){
		$apply=Db::table("wanneng_apply")->where("id=$aid")->find();
			$apply["feitime"]=date("Y-m-d",$apply["feitime"]);
			if($apply["feiuser"])
				$apply["feiuser"]=Db::table("employee")->where("id={$apply["feiuser"]}")->value("em_name");
			//print_r($apply);
			
			$eid=$apply["cuser"];
			$ctime=date("Y-m-d",$apply["ctime"]);

			$fujian=[];
			//print_r($apply);
			if($apply["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
				//print_r($fujian);
			}
			 $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
      
        $type=Db::table("wanneng_type")->where(["id"=>$apply["type"]])->value("title");
        $eid=Session::get("employee_id");
    		$users=Db::table("employee")->where(["id"=>$eid])->find();
	  Vendor("PHPExcel");
        
        
       Vendor("PHPExcel.Writer.Excel5");

       // Loader::import('PHPExcel.Classes.PHPExcel');
        //Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
       //  Vendor("Excel.PHPExcel.Writer.Excel2007");
        $objPHPExcel= new PHPExcel();
            
            
        $objProps=$objPHPExcel->getProperties();
            
        $objProps->setCreator('admin');
        $objProps->setLastModifiedBy('admin');
        $objProps->setTitle('Office 2003 XLSX Test Document');
        $objProps->setSubject('Office 2003 XLSX Test Document');
        $objProps->setDescription("Test document, generated by PHPExcel.");
        $objProps->setKeywords("office excel PHPExcel");
        $objProps->setCategory("Test");
            	
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle("万用申请完整版导出");
              #标题
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
 

       

        
          $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "万用申请完整版导出");
              //水平剧中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //垂直剧中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //字体
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);

            #################表头信息
        $objPHPExcel->getActiveSheet()->mergeCells('A2:G2');
        $objPHPExcel->getActiveSheet()->setCellValue('A2',"(本表单由软件导出，导出时间：".date("Y-m-d H:i:s")."；导出人：{$users["em_name"]})");
                //水平剧中
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //垂直剧中
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //加粗
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
        //字体
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);

        $objPHPExcel->getActiveSheet()->setCellValue('A3',"主题");
        $objPHPExcel->getActiveSheet()->mergeCells('B3:G3');
        $objPHPExcel->getActiveSheet()->setCellValue('B3',$apply["title"],PHPExcel_Cell_DataType::TYPE_STRING);
         $objPHPExcel->getActiveSheet()->getStyle('B3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

        $objPHPExcel->getActiveSheet()->setCellValue('A4',"姓名");
        $objPHPExcel->getActiveSheet()->setCellValue('B4',$user["em_name"],PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('C4',"编号",PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('D4',$apply["no"],PHPExcel_Cell_DataType::TYPE_STRING); 
        $objPHPExcel->getActiveSheet()->setCellValue('E4',"申请人部门",PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('F4',$user["de_name"],PHPExcel_Cell_DataType::TYPE_STRING); 
        $objPHPExcel->getActiveSheet()->mergeCells('F4:G4');

        $objPHPExcel->getActiveSheet()->setCellValue('A5',"申请时间");
        $objPHPExcel->getActiveSheet()->setCellValue('B5',$ctime,PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('C5',"申请类型",PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('D5',$type,PHPExcel_Cell_DataType::TYPE_STRING); 
        $objPHPExcel->getActiveSheet()->setCellValue('E5',"是否涉及费用",PHPExcel_Cell_DataType::TYPE_STRING);
        if($apply["iscost"])
        	$objPHPExcel->getActiveSheet()->setCellValue('F5',"是",PHPExcel_Cell_DataType::TYPE_STRING); 
        else
        	$objPHPExcel->getActiveSheet()->setCellValue('F5',"否",PHPExcel_Cell_DataType::TYPE_STRING); 
        $objPHPExcel->getActiveSheet()->mergeCells('F5:G5');

        $objPHPExcel->getActiveSheet()->setCellValue('A6',"内容");
        $objPHPExcel->getActiveSheet()->mergeCells('B6:G6');
        $objPHPExcel->getActiveSheet()->setCellValue('B6',strip_tags(trim($apply["neirong"])),PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->getStyle('B6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

        $logs=$this->getlog(6,$aid);
        $h=8;
         $objPHPExcel->getActiveSheet()->setCellValue('A'.$h,"审批意见");
            $objPHPExcel->getActiveSheet()->mergeCells("A{$h}:G{$h}");
             $objPHPExcel->getActiveSheet()->getStyle("A{$h}:G{$h}")->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
         $objPHPExcel->getActiveSheet()->getStyle("A{$h}:G{$h}")->getFill()->getStartColor()->setARGB('0099CCFF');

            for($i=0;$i<count($logs);$i++){
                $h++;
                if($logs[$i]["COMMENTS"])
                    $str="{$logs[$i]["USERNAME"]} 审批动作：{$logs[$i]["MEMO"]}；审批意见：{$logs[$i]["COMMENTS"]}(审批时间：{$logs[$i]["ACTIONTIME"]})";
                else
                    $str="{$logs[$i]["USERNAME"]} 审批动作：{$logs[$i]["MEMO"]}；审批意见：无 (审批时间：{$logs[$i]["ACTIONTIME"]})";
                $objPHPExcel->getActiveSheet()->setCellValue('A'.$h,$str);
                $objPHPExcel->getActiveSheet()->mergeCells("A{$h}:G{$h}");
            }

          $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
                $styleArray = array(  
        'borders' => array(  
            'allborders' => array(  
                //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的  
                'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框  
                //'color' => array('argb' => 'FFFF0000'),  
            ),  
        ),  
    );  
   $objPHPExcel->getActiveSheet()->getStyle('A1:G'.$h)->applyFromArray($styleArray);//这里就是画出从单元格A5到N5的边框，看单元格最右边在哪哪
        



        	  $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
           ob_end_clean();
        header("Pragma: public");
        header("Expires:0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=万用申请完整版导出.xls');
        header("Content-type:text/html;charset=utf-8");
        header("Content-Transfer-Encoding:binary");
        
        $objWriter->save('php://output');
	}
	public function daochu($aid){
		$apply=Db::table("wanneng_apply")->where("id=$aid")->find();
			$apply["feitime"]=date("Y-m-d",$apply["feitime"]);
			if($apply["feiuser"])
				$apply["feiuser"]=Db::table("employee")->where("id={$apply["feiuser"]}")->value("em_name");
			//print_r($apply);
			
			$eid=$apply["cuser"];
			$ctime=date("Y-m-d",$apply["ctime"]);

			$fujian=[];
			//print_r($apply);
			if($apply["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
				//print_r($fujian);
			}
			 $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
      
        $type=Db::table("wanneng_type")->where(["id"=>$apply["type"]])->value("title");
        $eid=Session::get("employee_id");
    		$users=Db::table("employee")->where(["id"=>$eid])->find();
	  Vendor("PHPExcel");
        
        
       Vendor("PHPExcel.Writer.Excel5");

       // Loader::import('PHPExcel.Classes.PHPExcel');
        //Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
       //  Vendor("Excel.PHPExcel.Writer.Excel2007");
        $objPHPExcel= new PHPExcel();
            
            
        $objProps=$objPHPExcel->getProperties();
            
        $objProps->setCreator('admin');
        $objProps->setLastModifiedBy('admin');
        $objProps->setTitle('Office 2003 XLSX Test Document');
        $objProps->setSubject('Office 2003 XLSX Test Document');
        $objProps->setDescription("Test document, generated by PHPExcel.");
        $objProps->setKeywords("office excel PHPExcel");
        $objProps->setCategory("Test");
            	
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle("万用申请详情");
              #标题
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);
        //$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
 

       

        
          $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "万用申请单详情");
              //水平剧中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //垂直剧中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //字体
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
         $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('0099CCFF');
        $objPHPExcel->getActiveSheet()->setCellValue('A2',"编号");
        $objPHPExcel->getActiveSheet()->mergeCells('B2:D2');
        $objPHPExcel->getActiveSheet()->setCellValue('B2',$apply["no"],PHPExcel_Cell_DataType::TYPE_STRING);


        $objPHPExcel->getActiveSheet()->setCellValue('A3',"申请人姓名");
        $objPHPExcel->getActiveSheet()->setCellValue('B3',$user["em_name"],PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('C3',"申请人部门",PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('D3',$user["de_name"],PHPExcel_Cell_DataType::TYPE_STRING);

        $objPHPExcel->getActiveSheet()->setCellValue('A4',"申请时间");
        $objPHPExcel->getActiveSheet()->setCellValue('B4',$ctime,PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('C4',"申请类型",PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()->setCellValue('D4',$type,PHPExcel_Cell_DataType::TYPE_STRING);

         $objPHPExcel->getActiveSheet()->setCellValue('A5',"主题");
        $objPHPExcel->getActiveSheet()->mergeCells('B5:D5');
        $objPHPExcel->getActiveSheet()->setCellValue('B5',$apply["title"],PHPExcel_Cell_DataType::TYPE_STRING);
         $objPHPExcel->getActiveSheet()->getStyle('B5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);


        $objPHPExcel->getActiveSheet()->setCellValue('A6',"内容");
        $objPHPExcel->getActiveSheet()->mergeCells('B6:D6');
        $objPHPExcel->getActiveSheet()->setCellValue('B6',strip_tags(trim($apply["neirong"])),PHPExcel_Cell_DataType::TYPE_STRING);


 $objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(60);
      //垂直剧中
        $objPHPExcel->getActiveSheet()->getStyle('A6:D6')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //字体
        $objPHPExcel->getActiveSheet()->getStyle('A2:D6')->getFont()->setSize(10);

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
           $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
           $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
           $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
                $styleArray = array(  
        'borders' => array(  
            'allborders' => array(  
                //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的  
                'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框  
                //'color' => array('argb' => 'FFFF0000'),  
            ),  
        ),  
    );  
   $objPHPExcel->getActiveSheet()->getStyle('A1:D6')->applyFromArray($styleArray);//这里就是画出从单元格A5到N5的边框，看单元格最右边在哪哪
        



        	  $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
           ob_end_clean();
        header("Pragma: public");
        header("Expires:0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=万用申请单详细.xls');
        header("Content-type:text/html;charset=utf-8");
        header("Content-Transfer-Encoding:binary");
        
        $objWriter->save('php://output');
	}
    public function shenhe($myId){
        $workfloawde=Db::table("cg_workflow")->where(["id"=>$myId])->find();
        $aid=$workfloawde["yw_id"];
        $this->assign("aid",$aid);
        $apply=Db::table("wanneng_apply")->where("id=$aid")->find();
        $state=$apply["state"];
        if($state==2){
            $this->redirect('Wanyongapply/zuofei', ['aid' => $aid,"from"=>2]);
        }else{
            $apply["feitime"]=date("Y-m-d",$apply["feitime"]);
            if($apply["feiuser"])
                $apply["feiuser"]=Db::table("employee")->where("id={$apply["feiuser"]}")->value("em_name");
            //print_r($apply);
            $this->assign("apply",$apply);
            $eid=$apply["cuser"];
            $ctime=date("Y-m-d",$apply["ctime"]);

            $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
             $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
            $this->assign("user",$user);
            $this->assign("ctime",$ctime);
            $this->assign("fujian",$fujian);
            $type=Db::table("wanneng_type")->where(["id"=>$apply["type"]])->value("title");
            $this->assign("type",$type);
             $logs=$this->getlog(6,$aid);
            //print_r($logs);
            $this->assign("logs",$logs);
            if($apply["iscost"]){
            $cost["bm"]=db("department")->where(["id"=>$apply["bm"]])->value("de_name");
            if($apply["kaohe"]){
                $kaohe=db("cg_kaoheqi2 k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->join("cg_cainian c","c.id=k1.cid")->where(["k2.id"=>$apply["kaohe"]])->field("k2.id,k2.title,c.title as cainian")->find();
                 $cost["kaohe"]=$kaohe["cainian"].'-'.$kaohe["title"];
            }else{
                $cost["kaohe"]="";
            }

           
            $cost["zhifu"]=db("cg_paymethod")->where(["id"=>$apply["zhifu"]])->value("title");
            $cost["shoukuan"]=db("employee")->where(["id"=>$apply["shoukuan"]])->value("em_name");
            $this->assign("cost",$cost);
            $detail=db("wanneng_detail")->where(["aid"=>$aid])->select();
            foreach ($detail as $k => $v) {
                # code...
                $detail[$k]["kemu"]=db("cg_feiyongkemu3")->where(["id"=>$v["feetype"]])->value("title");
            }
            $this->assign("detail",$detail);
        }
            return $this->fetch();
        }
            
    }
	public function detail($aid){
		$this->assign("aid",$aid);
		$apply=Db::table("wanneng_apply")->where("id=$aid")->find();
			$apply["feitime"]=date("Y-m-d",$apply["feitime"]);
			if($apply["feiuser"])
				$apply["feiuser"]=Db::table("employee")->where("id={$apply["feiuser"]}")->value("em_name");
			//print_r($apply);
			$this->assign("apply",$apply);
			$eid=$apply["cuser"];
			$ctime=date("Y-m-d",$apply["ctime"]);

			$fujian=[];
			//print_r($apply);
			if($apply["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
				//print_r($fujian);
			}
			 $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
        $this->assign("user",$user);
        $this->assign("ctime",$ctime);
        $this->assign("fujian",$fujian);
        $type=Db::table("wanneng_type")->where(["id"=>$apply["type"]])->value("title");
        $this->assign("type",$type);
         $logs=$this->getlog(6,$aid);
        //print_r($logs);
        $this->assign("logs",$logs);
        if($apply["iscost"]){
            $cost["bm"]=db("department")->where(["id"=>$apply["bm"]])->value("de_name");
            if($apply["kaohe"]){
                $kaohe=db("cg_kaoheqi2 k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->join("cg_cainian c","c.id=k1.cid")->where(["k2.id"=>$apply["kaohe"]])->field("k2.id,k2.title,c.title as cainian")->find();
                 $cost["kaohe"]=$kaohe["cainian"].'-'.$kaohe["title"];
            }else{
                $cost["kaohe"]="";
            }

           
            $cost["zhifu"]=db("cg_paymethod")->where(["id"=>$apply["zhifu"]])->value("title");
            $cost["shoukuan"]=db("employee")->where(["id"=>$apply["shoukuan"]])->value("em_name");
            $this->assign("cost",$cost);
            $detail=db("wanneng_detail")->where(["aid"=>$aid])->select();
            foreach ($detail as $k => $v) {
                # code...
                $detail[$k]["kemu"]=db("cg_feiyongkemu3")->where(["id"=>$v["feetype"]])->value("title");
            }
            $this->assign("detail",$detail);
        }
		return $this->fetch();
	}
    /*
	public function zuofei($aid){
		$apply=Db::table("wanneng_apply")->where("id=$aid")->find();
			//print_r($apply);
			$this->assign("apply",$apply);
			$eid=$apply["cuser"];
			$ctime=date("Y-m-d",$apply["ctime"]);

			$fujian=[];
			//print_r($apply);
			if($apply["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
				//print_r($fujian);
			}
			 $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
        $this->assign("user",$user);
        $this->assign("ctime",$ctime);
        $this->assign("fujian",$fujian);
        $type=Db::table("wanneng_type")->where(["id"=>$apply["type"]])->value("title");
        $this->assign("type",$type);
		return $this->fetch();
	}
	public function zuofeichuli(){
		$request = Request::instance();$data = $request->post();
		$up["state"]=8;
		$up["feiuser"]=Session::get("employee_id");
		$up["feitime"]=time();
		if(trim($data["reason"]))
			$up["feireason"]=trim($data["reason"]);
		else
			return 3;
		try{
			Db::table("wanneng_apply")->where(["id"=>$data["aid"]])->update($up);
		}catch (\Exception $e){
			return 2;
		}
		return 1;
	}*/

	public function shanchu($id){
		$res=Db::table("wanneng_apply")->where(["id"=>$id])->update(["is_deleted"=>1]);
		if($res)
			return 1;
		else
			return 0;
	}
	public function tijiao($id){
		$back=$this->gowork(6,$id);
        return $back;
	}
	//获得模版
	public function getmoban($mid){
		return Db::table("wanneng_moban")->where(["id"=>$mid])->value("neirong");
	}
	public function bmwanyong($page=1,$list=10){
		$request = Request::instance();$data = $request->post();
		$state=Db::table("ti_state")->order("paixu")->select();

        $this->assign("state",$state);
		$eid=Session::get("employee_id");
		$u=Db::table("employee")->alias("e")
		->join("department_employee de","de.employee_id=e.id")
		->join("department d","de.department_id=d.id")
		->where("e.id=$eid and de.index=1")
		->field("d.id,d.de_name")
		->find();
		$where="ap.is_deleted=0 and d.id={$u["id"]} and de.index=1 ";

		if($data){
			if(trim($data["no"])){
				$no=trim($data["no"]);
				$where.="and ap.no='$no' ";
			}
			if(trim($data["title"])){
				$title=trim($data["title"]);
				$where.="and ap.title='$title' ";
			}
			if(trim($data["st"])){
				$st=trim($data["st"]);
				$where.="and ap.state=$st ";
			}
			if(trim($data["start"])){
				$start=strtotime(trim($data["start"]));
				$where.="and ap.ctime>=$start ";
			}
			if(trim($data["end"])){
				$end=strtotime(trim($data["end"]));
				$where.="and ap.ctime<=$end ";
			}
		}else{
			$data=["no"=>"","title"=>"","st"=>"","start"=>"","end"=>""];
		}
		$sum=Db::table("wanneng_apply")->alias("ap")
		->join("employee e","ap.cuser=e.id")
        ->join("department_employee de","de.employee_id=e.id")
        ->join("department d","de.department_id=d.id")
        ->join("cg_workflow wf","ap.id=wf.yw_id","LEFT")
		->where($where)->count();
		 $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("wanneng_apply")->alias("ap")
        ->join("employee e","ap.cuser=e.id")
        ->join("department_employee de","de.employee_id=e.id")
        ->join("department d","de.department_id=d.id")
        ->join("ti_state s","ap.state=s.id")
        ->join("cg_workflow wf","ap.id=wf.yw_id","LEFT")
		->where($where)
		->order("ap.id desc")->limit($start,$list)
		->field("ap.id,ap.title,ap.no,ap.state,s.title as statename,e.em_name,d.de_name,ap.ctime,wf.id as myId")
		->select();
		for($i=0;$i<count($apply);$i++){
			$apply[$i]["ctime"]=date("Y-m-d",$apply[$i]["ctime"]);
            $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>6])->value("id");
		}
		$this->assign("apply",$apply);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
		return $this->fetch();
	}
	//我的万用
	public function mywanyong($page=1,$list=10){
		$request = Request::instance();$data = $request->post();
		$state=Db::table("ti_state")->order("paixu")->select();

        $this->assign("state",$state);
		$eid=Session::get("employee_id");
		$where="ap.is_deleted=0 and ap.cuser=$eid and de.index=1 ";

		if($data){
			if(trim($data["no"])){
				$no=trim($data["no"]);
				$where.="and ap.no='$no' ";
			}
			if(trim($data["title"])){
				$title=trim($data["title"]);
				$where.="and ap.title='$title' ";
			}
			if(trim($data["st"])){
				$st=trim($data["st"]);
				$where.="and ap.state=$st ";
			}
			if(trim($data["start"])){
				$start=strtotime(trim($data["start"]));
				$where.="and ap.ctime>=$start ";
			}
			if(trim($data["end"])){
				$end=strtotime(trim($data["end"]));
				$where.="and ap.ctime<=$end ";
			}
		}else{
			$data=["no"=>"","title"=>"","st"=>"","start"=>"","end"=>""];
		}
		$sum=Db::table("wanneng_apply")->alias("ap")
		->join("employee e","ap.cuser=e.id")
        ->join("department_employee de","de.employee_id=e.id")
        ->join("department d","de.department_id=d.id")
        
		->where($where)->count();
		 $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $apply=Db::table("wanneng_apply")->alias("ap")
        ->join("employee e","ap.cuser=e.id")
        ->join("department_employee de","de.employee_id=e.id")
        ->join("department d","de.department_id=d.id")
        ->join("ti_state s","ap.state=s.id")
        
		->where($where)
		->order("ap.id desc")->limit($start,$list)
		->field("ap.id,ap.title,ap.no,ap.state,s.title as statename,e.em_name,d.de_name,ap.ctime")
		->select();
		for($i=0;$i<count($apply);$i++){
			$apply[$i]["ctime"]=date("Y-m-d",$apply[$i]["ctime"]);
            $apply[$i]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$apply[$i]["id"],"ywlx"=>6])->value("id");
		}
		$this->assign("apply",$apply);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
		return $this->fetch();
	}
    public function selectKaoheqi()
    {
        return $this->fetch();
    }
     //作废/修改
    public function zuofei($aid,$from=1){
        $this->assign("from",$from);
        $wtype=Db::table("wanneng_type")->select();
        $this->assign("wtype",$wtype);
        $wmoban=Db::table("wanneng_moban")->select();
        $this->assign("wmoban",$wmoban);
        $this->assign("aid",$aid);
        $pay=Db::table("cg_paymethod")->select();
        $this->assign("pay",$pay);
        if($aid){
            $apply=Db::table("wanneng_apply")->where("id=$aid")->find();
            //print_r($apply);
            $this->assign("apply",$apply);
            $eid=$apply["cuser"];
            $ctime=date("Y-m-d",$apply["ctime"]);

            $fujian=[];
            //print_r($apply);
            if($apply["fujian"]){
                $fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
                //print_r($fujian);
            }
            if($apply["bm"])
                $bmid=$apply["bm"];
            else
                $bmid=db("department_employee")->where(["employee_id"=>$apply["cuser"],"index"=>1])->value("department_id");
            if($apply["kaohe"])
                $kaoheid=$apply["kaohe"];
            else{
                $time=time();
                $kaoheid=db("cg_kaoheqi2")->where("starttime<=$time and endtime>=$time")->value("id");
            }
            if($apply["shoukuan"]){
                $shoukuanid=$apply["shoukuan"];
            }else{
                $shoukuanid=$apply["cuser"];
            }
            $detail=db("wanneng_detail")->where("aid=$aid")->select();
        }
        $this->assign("detail",$detail);
        $this->assign("bmid",$bmid);
        $bm=db("department")->where(["id"=>$bmid])->value("de_name");
        $this->assign("bm",$bm);
        $this->assign("kaoheid",$kaoheid);
        $kao2=db("cg_kaoheqi2 k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")->where(["k2.id"=>$kaoheid])->field("k2.id,k2.title,c.title as cainian")->find();
        $kaohe=$kao2["cainian"].'-'.$kao2["title"];
        $this->assign("kaohe",$kaohe);
        $this->assign("shoukuanid",$shoukuanid);
        $shoukuan=db("employee")->where(["id"=>$shoukuanid])->value("em_name");
        $this->assign("shoukuan",$shoukuan);
        $where="esuseway like '%\"4\"%'";
        $fei3=db("cg_feiyongkemu3")->where($where)->field("id,title")->select();
        $this->assign("fei3",$fei3);
         $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
        $this->assign("user",$user);
        $this->assign("ctime",$ctime);
        $this->assign("fujian",$fujian);
         $logs=$this->getlog(6,$aid);
        //print_r($logs);
        $this->assign("logs",$logs);
        $btn=$this->getbton(6,$aid);
        $this->assign("btn",$btn);
        return $this->fetch();
    }
	//新增。修改
	public function add($aid=null){
		$wtype=Db::table("wanneng_type")->select();
		$this->assign("wtype",$wtype);
		$wmoban=Db::table("wanneng_moban")->select();
		$this->assign("wmoban",$wmoban);
		$this->assign("aid",$aid);
        $pay=Db::table("cg_paymethod")->select();
        $this->assign("pay",$pay);
		if($aid){
			$apply=Db::table("wanneng_apply")->where("id=$aid")->find();
			//print_r($apply);
			$this->assign("apply",$apply);
			$eid=$apply["cuser"];
			$ctime=date("Y-m-d",$apply["ctime"]);

			$fujian=[];
			//print_r($apply);
			if($apply["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
				//print_r($fujian);
			}
            if($apply["bm"])
                $bmid=$apply["bm"];
            else
                $bmid=db("department_employee")->where(["employee_id"=>$apply["cuser"],"index"=>1])->value("department_id");
            if($apply["kaohe"])
                $kaoheid=$apply["kaohe"];
            else{
                $time=time();
                $kaoheid=db("cg_kaoheqi2")->where("starttime<=$time and endtime>=$time")->value("id");
            }
            if($apply["shoukuan"]){
                $shoukuanid=$apply["shoukuan"];
            }else{
                $shoukuanid=$apply["cuser"];
            }
            $detail=db("wanneng_detail")->where("aid=$aid")->select();
		}else{
			$eid=Session::get("employee_id"); $ctime=date("Y-m-d");
            $bmid=db("department_employee")->where(["employee_id"=>$eid,"index"=>1])->value("department_id");
            $time=time();
            $kaoheid=db("cg_kaoheqi2")->where("starttime<=$time and endtime>=$time")->value("id");
            $shoukuanid=$eid;
			$fujian=[];
            $detail=[];
		}
        $this->assign("detail",$detail);
        $this->assign("bmid",$bmid);
        $bm=db("department")->where(["id"=>$bmid])->value("de_name");
        $this->assign("bm",$bm);
        $this->assign("kaoheid",$kaoheid);
        $kao2=db("cg_kaoheqi2 k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")->where(["k2.id"=>$kaoheid])->field("k2.id,k2.title,c.title as cainian")->find();
        $kaohe=$kao2["cainian"].'-'.$kao2["title"];
        $this->assign("kaohe",$kaohe);
        $this->assign("shoukuanid",$shoukuanid);
        $shoukuan=db("employee")->where(["id"=>$shoukuanid])->value("em_name");
        $this->assign("shoukuan",$shoukuan);
        $where="esuseway like '%\"4\"%'";
        $fei3=db("cg_feiyongkemu3")->where($where)->field("id,title")->select();
        $this->assign("fei3",$fei3);
		 $user=Db::table("employee")->alias("em")
            ->join("department_employee de","de.employee_id=em.id")
            ->join("department d","de.department_id=d.id")
            ->join("users u","u.employee_id=em.id")
            ->join("users_role ur","u.id=ur.users_id")
            ->join("role r","ur.role_id=r.id")
            ->where("em.id=$eid and de.index=1")
            ->field("em.em_name,d.de_name,em.mobile,em.id,r.role_name")
            ->find();
        $this->assign("user",$user);
        $this->assign("ctime",$ctime);
        $this->assign("fujian",$fujian);
		return $this->fetch();
	}
	//add处理
	public function addchuli(){
		$request = Request::instance();$data = $request->post();
        //print_r($data);exit;
		$adddata["type"]=$data["types"];
		if(trim($data["title"]))
			$adddata["title"]=trim($data["title"]);
		else
			return json_encode(["msg"=>"主题不能为空","state"=>2]);
		if(strip_tags(trim($data["neirong"])))
			$adddata["neirong"]=trim($data["neirong"]);
		else
			return json_encode(["msg"=>"内容不能为空","state"=>2]);
		$adddata["iscost"]=$data["iscost"];
		$adddata["fujian"]=$data["attach"];
		$adddata["state"]=$data["state"];
        if($data["iscost"]==1){
            $adddata["bm"]=$data["bmid"];
            $adddata["kaohe"]=$data["kaoheid"];
            $adddata["zhifu"]=$data["zhifu"];
            $adddata["shoukuan"]=$data["shoukuan"];
            if(isset($data["feetype"])&&isset($data["fee"])&&isset($data["remark"])){
                for($i=0;$i<count($data["feetype"]);$i++){
                    $detail[$i]["feetype"]=$data["feetype"][$i];
                    $detail[$i]["fee"]=$data["fee"][$i];
                    $detail[$i]["remark"]=$data["remark"][$i];
                }
            }else{
                return json_encode(["msg"=>"您选择了涉及费用，请填写明细","state"=>2]);
            }
        }else{
            $adddata["bm"]="";
            $adddata["kaohe"]='';
            $adddata["zhifu"]='';
            $adddata["shoukuan"]='';
            $detail=[];
        }
		if($data["aid"]){
			$adddata["puser"]=Session::get("employee_id");
			$adddata["ptime"]=time();

			$res1=Db::table("wanneng_apply")->where(["id"=>$data["aid"]])->update($adddata);
            db("wanneng_detail")->where(["aid"=>$data["aid"]])->delete();
            if($detail){
                foreach ($detail as $k => $v) {
                    # code...
                    $detail[$k]["aid"]=$data["aid"];
                }
                 db("wanneng_detail")->insertAll($detail);
            }
           
            $aid=$data["aid"];
		}else{
			try{
				 //生成单号
	            $timenow=date("Ymd");
	            $num=Db::table("wanneng_apply")->where("no like '$timenow%'")->count();
	            $num++;
	            $strnum=$timenow.sprintf("%05d", $num);
	            $adddata["no"]=$strnum;
	            $adddata["cuser"]=Session::get("employee_id");
	            $adddata["puser"]=Session::get("employee_id");
	            $adddata["ctime"]=time();
	            $adddata["ptime"]=time();
				$res1=Db::table("wanneng_apply")->insertGetId($adddata);
                $aid=$res1;
                if($detail){
                    foreach ($detail as $k => $v) {
                        # code...
                        $detail[$k]["aid"]=$aid;
                    }
                    db("wanneng_detail")->insertAll($detail);
                }

			}catch (\Exception $e){
       		
       			return json_encode(["msg"=>$e->getMessage(),"state"=>2]);
       		}

		}
		if($res1){
            if($data["state"]==3){
                $back=$this->work(6,$aid);
                $backarr=json_decode($back,true);
                if($backarr["state"]!=1){
                    Db::rollBack();
                    return $back;
                }
            }
			return json_encode(["msg"=>"","state"=>1]);
		}else{
			return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
		}

	}
    public function baocunchuli(){
        $request = Request::instance();$data = $request->post();
        $adddata["state"]=3;$aid=$data["aid"];$fid=6;
        $adddata["type"]=$data["types"];
        if(trim($data["title"]))
            $adddata["title"]=trim($data["title"]);
        else
            return json_encode(["msg"=>"主题不能为空","state"=>2]);
        if(strip_tags(trim($data["neirong"])))
            $adddata["neirong"]=trim($data["neirong"]);
        else
            return json_encode(["msg"=>"内容不能为空","state"=>2]);
        $adddata["iscost"]=$data["iscost"];
        $adddata["fujian"]=$data["attach"];$adddata["puser"]=Session::get("employee_id");
        $adddata["ptime"]=time();
        if($data["iscost"]==1){
            $adddata["bm"]=$data["bmid"];
            $adddata["kaohe"]=$data["kaoheid"];
            $adddata["zhifu"]=$data["zhifu"];
            $adddata["shoukuan"]=$data["shoukuan"];
            if(isset($data["feetype"])&&isset($data["fee"])&&isset($data["remark"])){
                for($i=0;$i<count($data["feetype"]);$i++){
                    $detail[$i]["feetype"]=$data["feetype"][$i];
                    $detail[$i]["fee"]=$data["fee"][$i];
                    $detail[$i]["remark"]=$data["remark"][$i];
                }
            }else{
                return json_encode(["msg"=>"您选择了涉及费用，请填写明细","state"=>2]);
            }
        }else{
            $adddata["bm"]="";
            $adddata["kaohe"]='';
            $adddata["zhifu"]='';
            $adddata["shoukuan"]='';
            $detail=[];
        }
        $res=$this->goworker($fid,$aid,$data["nodeKey"],$data["selectId"],$data["selectName"]);
        if($res["operateStatus"]=="success"){
            try{
                $res1=Db::table("wanneng_apply")->where(["id"=>$data["aid"]])->update($adddata);
                db("wanneng_detail")->where(["aid"=>$data["aid"]])->delete();
                if($detail){
                    foreach ($detail as $k => $v) {
                        # code...
                        $detail[$k]["aid"]=$data["aid"];
                    }
                     db("wanneng_detail")->insertAll($detail);
                }
            }catch (\Exception $e){
                  
                return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
            }
            if($res1){
                return json_encode(["msg"=>"","state"=>1]);
            }else{
                 
                return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
            }
            
        }else{
            return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
        }
    }
      //作废处理
    public function zuofeichuli(){
      $request = Request::instance();$data = $request->post();
      //print_r($data);exit;
      $fid=6;$aid=$data["aid"];$reason=$data["reason"];
      if(!trim($reason))
        return json_encode(["msg"=>"作废原因不能为空","state"=>2]);
      $res=$this->goworker($fid,$aid,$data["nodeKey"],$data["selectId"],$data["selectName"]);
      if($res["operateStatus"]=="success"){
        $up["feiuser"]=Session::get("employee_id");
        $up["feitime"]=time();
        $up["feireason"]=$reason;
        $up["id"]=$aid;
        $up["state"]=8;
        try{
          Db::table("wanneng_apply")->update($up);
        }catch (\Exception $e){
        
          return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
        }
      return json_encode(["msg"=>"","state"=>1]);
        
      }else{
        return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
      }
      
    }

}